package com.icinfo.cloud.provider.punish.strategy.document.enums;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.icinfo.cloud.provider.common.enums.DocumentKindEnum;
import com.icinfo.cloud.provider.common.exception.BusinessException;
import com.icinfo.cloud.provider.punish.strategy.document.*;
import com.icinfo.cloud.provider.punish.strategy.document.util.DocumentParamUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 直通车文书参数枚举
 *
 * @author Junfeng.Chen
 * @date 2024/1/12 16:44
 **/
@Getter
@AllArgsConstructor
public enum ZtcDocumentParamEnum {
    /**
     * 责令改正通知书
     */
    ZLGZTZS(DocumentKindEnum.DOC_KIND_ZLGZTZS, ZlgztzsDocumentParam.class),
    /**
     * 送达方式及地址确认书
     */
    SDDZQRS(DocumentKindEnum.DOC_KIND_SDDZQRS, SddzqrsDocumentParam.class),
    /**
     * 询问通知书
     */
    XWTZS(DocumentKindEnum.DOC_KIND_XWTZS, XwtzsDocumentParam.class),
    /**
     * 抽样取证通知书
     */
    CYQZTZS(DocumentKindEnum.DOC_KIND_CYQZTZS, CyqztzsDocumentParam.class),
    /**
     * 抽样取证物品通知书
     */
    CYQZWPCLTZS(DocumentKindEnum.DOC_KIND_CYQZWPCLTZS, CyqzwpcltzsDocumentParam.class),
    /**
     * 责令停止违法行为通知书
     */
    ZLTZWFXWTZS(DocumentKindEnum.DOC_KIND_ZLTZWFXWTZS, ZltzwfxwtzsDocumentParam.class),
    ;

    /**
     * 文书种类
     */
    private final DocumentKindEnum documentKind;

    /**
     * 文书转换参数对象
     */
    private final Class<? extends BaseDocumentParam> paramEntityClass;

    /**
     * 文书种类map
     */
    private static final Map<DocumentKindEnum, ZtcDocumentParamEnum> KIND_MAP;

    static {
        KIND_MAP = Arrays.stream(values()).collect(Collectors.toMap(ZtcDocumentParamEnum::getDocumentKind, Function.identity()));
    }

    /**
     * 根据文书种类获取文书转换枚举
     *
     * @param documentKind
     * @return
     */
    public static ZtcDocumentParamEnum getByKind(DocumentKindEnum documentKind) {
        ZtcDocumentParamEnum converter = KIND_MAP.get(documentKind);
        if (Objects.isNull(converter)) {
            throw new BusinessException("没有该文书种类的，文书参数转换方案");
        }
        return converter;
    }

    /**
     * 转换窄幅参数到宽幅参数
     *
     * @param documentKind 文书类型
     * @param params       文书参数
     * @return 转换后的参数
     */
    public static Map<String, Object> convertNarrowToStandardParams(DocumentKindEnum documentKind, Map<String, Object> params) {
        if (!KIND_MAP.containsKey(documentKind)) {
            return params;
        }
        //深克隆，以免对象变化
        JSONObject obj = JSON.parseObject(JSON.toJSONString(params));
        DocumentParamUtil.convertNarrowToStandard(obj, getByKind(documentKind).getParamEntityClass());
        return obj;
    }

    /**
     * 转换宽幅参数到窄幅参数
     *
     * @param documentKind 文书类型
     * @param params       文书参数
     * @return 转换后的参数
     */
    public static Map<String, Object> convertStandardToNarrowParams(DocumentKindEnum documentKind, Map<String, Object> params) {
        if (!KIND_MAP.containsKey(documentKind)) {
            return params;
        }
        //深克隆，以免对象变化
        JSONObject obj = JSON.parseObject(JSON.toJSONString(params));
        DocumentParamUtil.convertStandardToNarrow(obj, getByKind(documentKind).getParamEntityClass());
        return obj;
    }
}
